package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.commons.SystemParameter;
import com.suineg.pms.service.commoms.SystemParameterService;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 *
 *@author Suine
 */
public class SystemParameterIntercepter extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		
		checkForm(rrequest, mRowData);
		
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}
	
	/**
	 * 检查数据合法性
	 * <p>主要是唯一性</P>
	 * @param rrequest
	 * @param mRowData
	 */
	public void checkForm(ReportRequest rrequest,Map<String, String> mRowData){
		SystemParameterService service = AppConfig.getInstance().getWac().getBean(SystemParameterService.class);
		String name = mRowData.get(Messages.getString("SystemParameterIntercepter.0")); //$NON-NLS-1$
		String code = mRowData.get(Messages.getString("SystemParameterIntercepter.1")); //$NON-NLS-1$
		String id = mRowData.get(Messages.getString("SystemParameterIntercepter.2")); //$NON-NLS-1$
		
		SystemParameter parameter = null;
		
		if(id == null || id.isEmpty()){	//插入
			check(rrequest, name, code);
		}else{		//修改
			parameter = service.get(Long.valueOf(id));
			if(parameter.getName().equals(name) || parameter.getCode().equals(code)){
				if(parameter.getName().equals(name)){
					check(rrequest, null, code);
				}else{
					check(rrequest, name, null);
				}
			}else{
				check(rrequest, name, code);
			}
		}
		
	}
	
	public void check(ReportRequest rrequest,String name,String code){
		List<SystemParameter> list = null;
		SystemParameterService service = AppConfig.getInstance().getWac().getBean(SystemParameterService.class);
		
		list = service.findByName(name);
		if(list != null && !list.isEmpty()){
			rrequest.getWResponse().getMessageCollector().warn(Messages.getString("SystemParameterIntercepter.3"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
		}
		
		list = service.findByCode(code);
		if(list != null && !list.isEmpty()){
			rrequest.getWResponse().getMessageCollector().warn(Messages.getString("SystemParameterIntercepter.4"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
		}
		
	}
	

}

